<?php
/**
 * Created by PhpStorm.
 * User: sunzexin
 * Date: 2018/1/5
 * Time: 12:30
 */

namespace App\Http\Logics\MiniProgramAdmin;

use App\Mark;
use App\Http\Logics\BaseLogic;
class MarkLogic extends BaseLogic
{
    /**
     * 创建咖啡馆标签
     * @param $data 咖啡馆标签信息
     * @return mixed 返回结果
     */
    public static function createMark($data)
    {
        try{
            $mark = new Mark();

            $mark->name = $data['name'];

            if (!$mark->save()){
                return self::response(self::OBJECT_SAVE_FAIL);
            }
            return self::response(self::SUCCESS, $mark);
        }catch (\Exception $e){
            return self::response(self::SYSTEM_ERROR);
        }
    }

    /**
     * 获取咖啡馆标签列表
     * @return mixed 返回结果
     */
    public static function getMarkList($data)
    {
        try{
            $query = Mark::where(function ($query) use ($data){
                if (isset($data['is_menu']) && $data['is_menu']){
                    $query->where('is_menu', $data['is_menu']);
                }
                if (isset($data['is_common']) && $data['is_common']){
                    $query->where('is_common', $data['is_common']);
                }
            })->orderBy('id', 'desc');

            $mark = $data['limit'] ? $query->paginate($data['limit']) : $query->get();

            if (empty($mark)){
                return self::response(self::CONTENT_IS_EMPTY);
            }
            return self::response(self::SUCCESS, $mark);
        }catch (\Exception $e){
            return self::response(self::SYSTEM_ERROR);
        }
    }

    public static function setCafeStatus($data)
    {
        $mark = Mark::find($data['mark_id']);
        if (empty($mark)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        $mark->status = !$mark->status;
        if (!$mark->save()){
            return self::response(self::OBJECT_SAVE_FAIL);
        }
        return self::response(self::SUCCESS);
    }

    /**
     * 通过城市编码获取使用最多的标签列表
     * @param $data
     * @return mixed
     */
    public static function getMostMarkList($data)
    {
        $query = Mark::withCount(['cafe' => function ($query) use ($data){
            if (isset($data['citycode']) && $data['citycode']){
                $query->where('citycode', $data['citycode']);
            }
        }])->orderBy('cafe_count', 'desc');

        $mark = $data['limit'] ? $query->paginate($data['limit']) : $query->get();

        // 参数有显示数量并且不分页
        if (isset($data['show_num']) && $data['show_num'] && !$data['limit']){
            $mark = $mark->take($data['show_num']);
        }

        if (empty($mark)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        return self::response(self::SUCCESS, $mark);
    }

    public static function setMarkIsCommon($data)
    {
        $mark = Mark::find($data['mark_id']);
        if (empty($mark)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        $mark->is_common = !$mark->is_common;
        if (!$mark->save()){
            return self::response(self::OBJECT_SAVE_FAIL);
        }
        return self::response(self::SUCCESS);
    }

    public static function getMarkDetail($data)
    {
        $mark = Mark::find($data['mark_id']);
        if (empty($mark)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        return self::response(self::SUCCESS, $mark);
    }
}